<?
$tmppath="../../";
REQUIRE ($tmppath."include/login.php");
REQUIRE ($tmppath."$templates/up.php");
REQUIRE($tmppath."protect/connect.php");
REQUIRE("../fonction.php");
$titre="Rechercher";
TITRE_JEU($titre);



//chercher les invention du joueur
//********************************
$query="SELECT con_invention FROM sdconnp where con_id=$userperso";
$resultat=mysql_query($query);
$tmp=mysql_fetch_row($resultat);
$inventiontmp=EXPLODE("/",$tmp[0]);

FOREACH($inventiontmp as $element){
    if ($drap==1){ $querytmp.=" OR ";}ELSE{$querytmp.=" WHERE (";}
    $i=$element;
    $invention[$i]=1;
    $querytmp.="invention_precedent=$i";
    $drap=1;
}    //Fin du foreach

//Aller chercher dans la DB invention les invention qu'il pourrais avoir accessible.
//**********************************************************************************
$query="SELECT invention_id, invention_name,invention_prerequis,invention_icon,invention_cout,invention_descr FROM invention $querytmp ) AND invention_categorie!='magie'";
$resultat=mysql_query($query);

//Aller chercher les invention qu'on invente deja.
//************************************************
$query="SELECT invention_invid FROM tmpinvention where invention_user=$userperso";
$resultat2=mysql_query($query);
WHILE ($request2=mysql_fetch_array($resultat2)){
      $tmp_id=$request2[0];
      $recherche[$tmp_id]=1;
}//Fin du while pour les recherches en cours.




$total=0;

WHILE ($request=mysql_fetch_array($resultat)){

$idinvention=$request[0];
$prerequis=EXPLODE("/",$request[2]);
$binvention=0;

      IF ($recherche[$idinvention]==1){
           $binvention=1;

      }
     
	  IF (!empty($prerequis[0])){
       FOREACH($prerequis as $element){


              IF ($invention[$element]!=1){
                $binvention=1;

            }
            
   }
            
       }//Fin du foreach


IF ($binvention==0 && $invention[$idinvention]!=1){
	$total++;
    $inventionid[$total]=$request[0];
	$iname[$request[0]]=$request[1];
	$ipic[$request[0]]=$request[3];
	$itmps[$request[0]]=$request[4];
    $idescription[$request[0]]=$request[5];
	
}
       
}//Fin du while.


//*************************************************************************************

//************************************
//****REcherche de la population******
//************************************

$query="SELECT  ville_name, ville_population,  ville_tcriminalite ,ville_t_occupe,ville_peuple_cara from sdville  where ville_id=$ville";
// 0= nom  1 = population total  2= taux criminalite 3= tribut occupee 4=ville caracteristique
$resultat=mysql_query($query);
$tmp=mysql_fetch_row($resultat);


$cara=explode("/",$tmp[4]);
$ville_int=$cara[3];

$criminalite=$tmp[2];
IF ($criminalite!=0){
$tribu_inexe=floor(($tmp[1]*($criminalite/100))/100);
}
else{
$tribu_inexe=0;
}


$population_libre=$tmp[1];
$tribu_total=floor($population_libre/100);
$tribu_libre=$tribu_total-$tmp[3]-$tribu_inexe;
//******************************************************************
//*Calculer combien de tribu peuvent etre mise dans les recherches.*
//******************************************************************


//REgarde combien de tribu est utiliser pour les inventions.
//*********************************************************
$query="SELECT SUM(invention_tribu) FROM tmpinvention WHERE invention_locid='$ville' AND invention_user=$userperso AND invention_loc='v'";
$tribu_use=0;
IF($resultat2=mysql_query($query)){
	$tmp=mysql_fetch_array($resultat2);

        IF (!empty($tmp[0])){
			$tribu_use=$tmp[0];
		}
    	ELSE{
	  		$tribu_use=0;
		}
}

$query="SELECT  batiment_niv, batiment_unite, batiment_unite_occuper FROM sdbatiment  WHERE batiment_ville=$ville AND batiment_user=$userperso AND batiment_style='atelier'";
// 0= niv  1 = unite  2= occuper 


$resultat=mysql_query($query);
$atelier_total=0;
$atelier_max=0;
//On va chercher les infos et les mettre dans les DB.
//**************************************************
WHILE ($request=mysql_fetch_array($resultat)){
	$inv_act+=$request[1];

	$atelier_total++;
	$atelier_max+=$request[0];
	
}

$inv_libre=($inv_act*100)-$tribu_use;

IF($atelier_max>=$tribu_libre){
	$atelier_max=$inv_act;
}

$atelier_min=$atelier_max-($atelier_max-(CEIL($tribu_use/100)));

IF ($tribu_use>$atelier_min){
	$atelier_min=ceil($tribu_use/100);
}
//Inventeur libre






//*****************************************************************

mysql_close;

?>

<!***********************************************************************
***********Les script javascript de la page***************************
***********************************************************************>

<script language="JavaScript" type="text/JavaScript">
<!--

tribulibre=<? echo $tribu_libre ?>;
intelligence=<? echo $ville_int ?>;
invmax=<? echo $atelier_max ?>;
invmin=<? echo $atelier_min ?>;
inv_libre=<? echo $inv_libre ?>;


image=new Array(<? echo $total ?>);
iname=new Array(<? echo $total ?>);
description=new Array(<? echo $total ?>);
tmpsinv=new Array(<? echo $total ?>);


<?

echo"image[0]=\"vide.gif\";";
//Pour le chargement des images dans les variable javascript.
//***********************************************************
FOR ($i=1; $i< $total+1; $i++){
$bonnumero=$inventionid[$i];
echo"image[$bonnumero]=\"$ipic[$bonnumero]\";";
}
//Pour la chargement des nomsdans les variable javascript.
//*************************************************************************
echo"iname[0]=\"Choississez\";";
FOR ($i=1; $i< $total+1; $i++){
$bonnumero=$inventionid[$i];
echo"iname[$bonnumero]=\"$iname[$bonnumero]\";";

}

//Pour la chargement de la description dans les variable javascript.
//******************************************************************
echo"description[0]=\"N/A\";";
FOR ($i=1; $i< $total+1; $i++){
$bonnumero=$inventionid[$i];
echo"description[$bonnumero]=\"$idescription[$bonnumero]\";";
}
//Pour la chargement du temps de construction dans les variable javascript.
//*************************************************************************
echo"tmpsinv[0]=0;";
FOR ($i=1; $i< $total+1; $i++){
$bonnumero=$inventionid[$i];
echo"tmpsinv[$bonnumero]=\"$itmps[$bonnumero]\";";
}
?>
function add(){
var tribul=document.form1.tribul.value;
var tribu=document.form1.tribu.value;
		
	if (tribul!=0){
		tribu=tribu-(-10);
		tribul=tribul-10;
		
		document.form1.tribu.value=tribu;
		document.form1.tribul.value=tribul;
	}

	if (document.form1.invention.value!=0 && document.form1.tribu.value!=0){
			chgtour(); 
		}


 
	
}

function sous(){
var tribul=document.form1.tribul.value;
var tribu=document.form1.tribu.value;
		
		if (document.form1.tribu.value!=0){
			document.form1.tribu.value=tribu-10;
			document.form1.tribul.value=tribul-(-10);
		}	

	if (document.form1.invention.value!=0 && document.form1.tribu.value!=0){
			chgtour() 
		}

}

function onch(){
		document.form1.tribul.value= inv_libre-document.form1.tribu.value;
		if (document.form1.tribu.value<0){ document.form1.tribu.value=0; document.form1.tribul.value=inv_libre; }
		if (document.form1.tribu.value>inv_libre){ document.form1.tribu.value=inv_libre;document.form1.tribul.value=0; }

	if (document.form1.invention.value!=0 && document.form1.tribu.value!=0){
			chgtour() 
		}
}

function chgimg(){
		document.imgbatiment.src = "../../images/invention/"+image[document.form1.invention.value];

		
		if (document.form1.invention.value!=0 && document.form1.tribu.value!=0){
			chgtour() 
		}
		
		document.form2.description.value=description[document.form1.invention.value];
		chgtmp();
		
		document.form2.prix.value=tmpsinv[document.form1.invention.value]+" Lorins";		

}	

function chgtmp(){
	document.form2.tmpc.value=tmpsinv[document.form1.invention.value];
}

function chgtour(){
 document.form2.tourc.value=Math.ceil(tmpsinv[document.form1.invention.value]/(intelligence*(document.form1.tribu.value/10)));;
}

function addinv(){

if (document.form3.tribui.value<invmax){
			document.form3.tribui.value++;
}

}

function souinv(){

if (document.form3.tribui.value>invmin){
			document.form3.tribui.value--;
}

}

		
		
			

//-->
</script>
<!/***********************************************************************
******************************Fin des javascripts***********************
***********************************************************************>
<div align="center">
<?
if (!empty($erreur)){
echo"<table width=\"85%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#000000\" background=\"".$tmppath."$templates/FondS.jpg\"><tr><td><strong><font color=\"#FF0000\"><p align=\"center\">$erreur</p></font></strong></td></table><BR>";
}


?>
  <table width="75%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td><table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
          <tr>
            <td><br>
              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr> 
                  <td height="27"><div align="center">Vous avez <? echo $atelier_total?> 
                      atelier. Tribu maximum : <? echo $atelier_max?> . Tribu 
                      minimum: <? echo $atelier_min;?> . </div>
                    <div align="center"><br>
                      <form name="form3" method="post" action="batiment_test.php">
                        <input type="button" name="Submit222" value="- " onClick="souinv()">
                        <input name="tribui" type="text" id="tribui" value="<? echo $inv_act ;?>" size="10" maxlength="5" READONLY>
                        <input type="button" name="Submit23" value="+" onClick="addinv()">
                        Tribu(s) employ&eacute;e(s) sur <? echo $atelier_max?><br>
                        <br>
                        <?
	echo"<input name=\"mode\" type=\"hidden\" id=\"mode\" value=\"inventeur\">";
	echo"<input name=\"ville\" type=\"hidden\" id=\"ville\" value=\"$ville\">";
	echo"<input name=\"$SID\" type=\"hidden\" id=\"$SID\" value=\"$SID\">";
	echo"<input name=\"userperso\" type=\"hidden\" id=\"userperso\" value=\"$userperso\">";
    echo"<input name=\"atelier_max\" type=\"hidden\" id=\"atelier_max\" value=\"$atelier_max\">";
    echo"<input name=\"inv_act\" type=\"hidden\" id=\"inv_act\" value=\"$inv_act\">";
 
 
	?>
                        <input type="submit" name="Submit3" value="Envoyer">
                      </form>
                      
                      
                    </div></td>
                  <td><div align="center"></div></td>
                </tr>
              </table> </td>
          </tr>
        </table>
        <form method="post" name="form1" action="batiment_test.php" target="_self">
          <div align="center"></div>
          <table width="300" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#333333">
            <tr> 
              <td width="141"><div align="right">inventeurs libre :</div></td>
              <td width="153"> 
                <input name="tribul" type="text" id="tribul" value="<? echo $inv_libre ?>" size="10" maxlength="5"  READONLY></td>
            </tr>
          </table>
          <p align="center"><strong>Invention :</strong></p>
          <table width="75%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="40%"><div align="center"><img src="../../images/vide.gif" name="imgbatiment" width="50" height="50" border="1" align="middle" id="imgbatiment"></div></td>
              <td width="60%"><div align="center"> 
                  <select name="invention" size="7" id="invention" onChange="chgimg()" >
                    <option value="0" selected>Choisissez l'invention</option>
                    <?
				  //**********************************
                  //***On met les nom dans l'option.**
                  //***********************************
                  
				   FOR ($i=1; $i< $total+1; $i++){


                        $bonnumero=$inventionid[$i];
                        
						echo"<option value=\"$bonnumero\">$iname[$bonnumero]</option></br>";
				   
				   }
				  
                  ?>
                  </select>
                </div></td>
            </tr>
          </table>
          <br>
          <table width="75%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td><div align="right">Combien d'inventeur ?</div></td>
              <td><div align="center">
                  <input type="button" name="Submit22" value="- " onClick="sous()">
                  <input name="tribu" type="text" id="tribu" value="0" size="5" maxlength="3" onChange="onch()">
                  <input type="button" name="Submit2" value="+" onClick="add()">
                </div></td>
            </tr>
          </table>
          <p align="center"> 
     <?
	echo"<input name=\"ville\" type=\"hidden\" id=\"ville\" value=\"$ville\">";
    echo"<input name=\"mode\" type=\"hidden\" id=\"mode\" value=\"recherche\">";
	echo"<input name=\"$SID\" type=\"hidden\" id=\"$SID\" value=\"$SID\">";
	echo"<input name=\"userperso\" type=\"hidden\" id=\"userperso\" value=\"$userperso\">";
    echo"<input name=\"tribu_libre\" type=\"hidden\" id=\"tribu_libre\" value=\"$tribu_libre\">";
	 echo"<input name=\"inv_libre\" type=\"hidden\" id=\"inv_libre\" value=\"$inv_libre\">";
 
	?>
            <input type="submit" name="Submit" value="Envoyer" >
          </p>
          </form>
        <form name="form2" method="post" action="">
          <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
            <tr>
              <td><div align="center">Temps de recherche:<br>
                  <input name="tmpc" type="text" id="tmpc" value="0" size="10" READONLY>
                  <br>
                  Intelligence de la ville :<br>
                  <input name="intelligence" type="text" id="intelligence" value="<? echo $ville_int ?>" size="10" maxlength="10" READONLY>
                  <br>
                  Tour pour la recherche :<br>
                  <input name="tourc" type="text" id="tourc" value="0" size="10" maxlength="10" READONLY>
                  <br>
                  Prix pour la recherche :<br>
                  <input name="prix" type="text" id="prix" value="0" size="10" maxlength="10" READONLY>
                  <br>
                  <br>
                </div></td>
              <td width="0" height="0"> <div align="center">
                  <table width="100%" border="0" align="center" cellspacing="0">
                    <tr> 
                      <td><div align="center"></div></td>
                      <td><div align="center"></div></td>
                    </tr>
                    <tr> 
                      <td><div align="center">
                          <input name="sorte1" type="text" id="sorte1">
                        </div></td>
                      <td><div align="center">
                          <input name="sorte2" type="text" id="sorte2">
                        </div></td>
                    </tr>
                  </table>
                  Descrition:<br>
                  <textarea name="description" cols="50" rows="4" id="description" READONLY></textarea>
                </div></td>
            </tr>
          </table>
          </form>
        
        <div align="center"><img src="../../templates/default/ligne.png" width="390" height="35"></div></td>
    </tr>
  </table>
  <p align="center">
  <table width="75%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000000">
    <tr>
      <td><div align="center"><strong><em><u>Recherche en cours.</u><br>
<? 
$query="SELECT invention_tribu,invention_invid,invention_avancement from tmpinvention where invention_locid=$ville";
$resultat=mysql_query($query);

echo"
<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> 
<tr>
 <td width=\"50%\" background=\"../../templates/default/FondS.jpg\"><div align=\"center\"><strong>Nom</strong></div></td>
 <td width=\"25%\" background=\"../../templates/default/FondS.jpg\"><div align=\"center\"><strong>Tribu(s)</strong></div></td>
 <td width=\"25%\" background=\"../../templates/default/FondS.jpg\"><div align=\"center\"><strong>Avancement</strong></div></td>
</tr>"; 

while ($element =mysql_fetch_array($resultat) ){
//Trouver le nom de la construction.
//**********************************
$query="SELECT invention_name, invention_cout from invention where invention_id=$element[1]";
// 0 = nom - 1 = temps.
$resultat2=mysql_query($query);
$tmp=mysql_fetch_row($resultat2);
$temps_construction=floor(($element[2]/$tmp[1])*100);
$nom_construction=$tmp[0];

ECHO"<tr>
          <td>$nom_construction</td>
          <td>$tribu</td>
          <td>$temps_construction %  
          </tr>";


}
?>          
		  
		  
          </em> </strong> </div></td>
    </tr>
  </table>
  
  
  
</div>
</body></html>
